package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.Console;

public class ConsoleView extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JTextField freq ;
	JButton start;
	JButton stop;
	Console c;
	JSlider framesPerSecond ;
	static final int FPS_MIN = 0;
	static final int FPS_MAX = 5;
	static final int FPS_INIT = 2;
	
	public ConsoleView(final String title){
		super();
		
		this.setTitle(title);
		StartListener startTimer  = new StartListener();
		StopListener stopTimer  = new StopListener();
		
		JPanel jp = (JPanel)this.getContentPane();
		
		c = new Console();
		
		framesPerSecond = new JSlider(JSlider.HORIZONTAL,
                FPS_MIN, FPS_MAX, FPS_INIT);
		framesPerSecond.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent arg0) {
				if(arg0.getSource().getClass().equals(framesPerSecond.getClass())){
					c.start(framesPerSecond.getValue());
				}
			}
			
		});

		//Turn on labels at major tick marks.
		framesPerSecond.setMajorTickSpacing(10);
		framesPerSecond.setMinorTickSpacing(1);
		framesPerSecond.setPaintTicks(true);
		framesPerSecond.setPaintLabels(true);
		
		freq = new JTextField();
		
		start= new JButton("Start");
		start.setSize(5000, 500);
		start.addActionListener(startTimer);
		
		stop = new JButton("Stop");
		stop.setSize(5, 5);
		stop.addActionListener(stopTimer);
	
		jp.add(framesPerSecond,BorderLayout.NORTH);
		jp.add(start,BorderLayout.WEST);
		jp.add(stop,BorderLayout.EAST);
		
	
		this.setSize(150, 100);
		this.setVisible(true);
		this.setLocation(200, 100);
	}
		
	private class StartListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
//			if(freq != null && freq.getText()!="" && freq.getText() != null)
				c.start(framesPerSecond.getValue());
			
		}
	}
	
	private class StopListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			c.stop();
			
		}
	}

	public Console getC() {
		return c;
	}

	public void setC(Console c) {
		this.c = c;
	}
	
	
}
